package com.ryder.petmatediarybackend.module.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ryder.petmatediarybackend.common.enums.ResultCode;
import com.ryder.petmatediarybackend.common.exception.BusinessException;
import com.ryder.petmatediarybackend.module.auth.dto.RoleDTO;
import com.ryder.petmatediarybackend.module.auth.entity.SysRole;
import com.ryder.petmatediarybackend.module.auth.mapper.SysRoleMapper;
import com.ryder.petmatediarybackend.module.auth.service.SysRoleService;
import com.ryder.petmatediarybackend.module.auth.service.SysUserRoleService;
import com.ryder.petmatediarybackend.module.auth.vo.RoleVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 描述：系统角色表(SysRole)表服务实现类
 *
 * @author Ryder
 * @version 1.0
 * @since 1.0
 * 更新日期: 2025/10/26
 */
@Service("sysRoleService")
@RequiredArgsConstructor
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    private final SysUserRoleService sysUserRoleService;

    @Override
    public RoleVO addRole(RoleDTO dto) {
        // 1. 校验角色编码是否已存在
        if (existsByCode(dto.getCode())) {
            throw new BusinessException(ResultCode.ROLE_CODE_EXISTS);
        }

        // 2. 创建角色实体
        SysRole role = new SysRole();
        BeanUtils.copyProperties(dto, role);
        role.setStatus(1); // 默认启用

        // 3. 保存
        this.save(role);

        // 4. 转换为 VO 返回
        RoleVO vo = new RoleVO();
        BeanUtils.copyProperties(role, vo);
        return vo;
    }

    @Override
    public RoleVO updateRole(Long id, RoleDTO dto) {
        // 1. 查询角色是否存在
        SysRole role = this.getById(id);
        if (role == null) {
            throw new BusinessException(ResultCode.ROLE_NOT_FOUND);
        }

        // 2. 如果修改了编码，检查新编码是否已存在
        if (!role.getCode().equals(dto.getCode()) && existsByCode(dto.getCode())) {
            throw new BusinessException(ResultCode.ROLE_CODE_EXISTS);
        }

        // 3. 更新
        BeanUtils.copyProperties(dto, role);
        this.updateById(role);

        // 4. 转换为 VO 返回
        RoleVO vo = new RoleVO();
        BeanUtils.copyProperties(role, vo);
        return vo;
    }

    @Override
    public void deleteRole(Long id) {
        // 1. 查询角色是否存在
        SysRole role = this.getById(id);
        if (role == null) {
            throw new BusinessException(ResultCode.ROLE_NOT_FOUND);
        }

        // 2. 检查是否有用户绑定
        if (sysUserRoleService.hasUsersBoundToRole(id)) {
            throw new BusinessException(ResultCode.ROLE_HAS_USERS);
        }

        // 3. 逻辑删除
        this.removeById(id);
    }

    @Override
    public List<RoleVO> listRoles() {
        // 查询所有角色
        List<SysRole> roles = this.list();

        // 转换为 VO
        return roles.stream().map(role -> {
            RoleVO vo = new RoleVO();
            BeanUtils.copyProperties(role, vo);
            return vo;
        }).collect(Collectors.toList());
    }

    @Override
    public RoleVO getRoleById(Long id) {
        SysRole role = this.getById(id);
        if (role == null) {
            throw new BusinessException(ResultCode.ROLE_NOT_FOUND);
        }

        RoleVO vo = new RoleVO();
        BeanUtils.copyProperties(role, vo);
        return vo;
    }

    @Override
    public SysRole getByCode(String code) {
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysRole::getCode, code);
        return this.getOne(wrapper);
    }

    @Override
    public boolean existsByCode(String code) {
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysRole::getCode, code);
        return this.count(wrapper) > 0;
    }
}
